<template>
  <div>
    <h2 style="background-color:red"> app 组件</h2>

    <van-button type="primary" />

    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <van-field v-model="username" name="loginname" label="用户名" placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]" />
        <van-field v-model="password" type="password" name="password" label="密码" placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]" />
      </van-cell-group>
      <div style="margin: 16px;">
        <van-button round block type="primary" native-type="submit">
          提交
        </van-button>
      </div>
    </van-form>




  </div>
</template>

<script setup lang="ts">
import { showNotify, closeNotify } from 'vant';
import { ref } from 'vue'

import { getLogin } from '@/api/api'




// 引入路由 
import { useRouter } from 'vue-router'


// 引入 pinia

import useUserStore from '@/pinia/index'
const userStore = useUserStore()

const router = useRouter()

const username = ref('admin');
const password = ref(123456);
const onSubmit = async (values: any) => {
  console.log('submit', values);
  let { data: { token, userid } } = await getLogin(values)

  // 登录成功
  // 1.1 存储token 用户信息name  id  
  //     token  存储地方  本地缓存   vuex/pinia 为了实现免登录

  userStore.setToken(token)
  userStore.setUserId(userid)

  // 1.2  跳转到home 页面
  router.replace({ name: 'home' })
};


</script>

<style scoped lang='scss'>

</style>